package nsu.edu.zsq.controller.sys;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import nsu.edu.zsq.bean.SysAcl;
import nsu.edu.zsq.bean.SysUser;
import nsu.edu.zsq.common.JsonData;
import nsu.edu.zsq.param.AclTreeParam;
import nsu.edu.zsq.service.sys.SysUserManagerService;

/** 
* Description: 管理员账号管理的controller类<br>
*/
@RequestMapping("/sys/sysUser")
@Controller
public class SysUserManagerController {

    @Autowired
    private SysUserManagerService sysUserManagerService;
    
    /**
     * 获取所有管理员账号信息
     * @return JsonData
     */
    @RequestMapping("/listSysUser")
    @ResponseBody
    public JsonData listSysUser() {
        List<SysUser> listSysUser = sysUserManagerService.listSysUser();
        return JsonData.success(listSysUser);
    }
    
    /**
     * 保存一条管理员账号信息
     * @param sysUser 管理员账号信息
     * @return JsonData
     */
    @RequestMapping("/saveSysUser")
    @ResponseBody
    public JsonData saveSysUser(SysUser sysUser) {
        sysUserManagerService.saveSysUser(sysUser);
        return JsonData.success();
    }
    
    /**
     * 跳转到编辑管理员信息页面
     * @param sysUserId 管理员id
     * @param model
     * @return String manager/sysUser/sysUser_edit.html
     */
    @RequestMapping("/toSysUser_edit")
    public String toSysUser_edit(@RequestParam("sysUserId")int sysUserId, Model model) {
        SysUser sysUser = sysUserManagerService.getSysUser(sysUserId);
        model.addAttribute("sysUser", sysUser);
        return "manager/sysUser/sysUser_edit.html";
    }
    
    /**
     * 编辑一条管理员账号信息
     * @param sysUser 管理员账号信息
     * @return JsonData
     */
    @RequestMapping("/editSysUser")
    @ResponseBody
    public JsonData editSysUser(SysUser sysUser) {
        sysUserManagerService.updateSysUser(sysUser);
        return JsonData.success();
    }
    
    /**
     * 跳转到修改管理员账号密码页面
     * @param sysUserId 管理员id
     * @param model
     * @return String manager/sysUser/sysUser_updatePassword.html
     */
    @RequestMapping("/toSysUser_updatePas")
    public String toSysUser_updatePas(@RequestParam("sysUserId")int sysUserId, Model model) {
        model.addAttribute("sysUserId", sysUserId);
        return "manager/sysUser/sysUser_updatePas.html";
    }
    
    /**
     * 更新一条管理员账号密码
     * @param sysUser 管理员账号信息
     * @return JsonData
     */
    @RequestMapping("/updatePassword")
    @ResponseBody
    public JsonData updatePassword(SysUser sysUser) {
        sysUserManagerService.updatePassword(sysUser);
        return JsonData.success();
    }
    
    /**
     * 单个/批量删除管理员账号
     * @param sysUserIds 管理员账号id数组
     * @return JsonData
     */
    @RequestMapping("/delSysUser")
    @ResponseBody
    public JsonData delSysUser(@RequestParam("sysUserIds[]")int[] sysUserIds) {
        sysUserManagerService.delSysUser(sysUserIds);
        return JsonData.success();
    }
    
    /**
     * 跳转到权限管理页面
     * @param sysUserId 管理员id
     * @param model
     * @return String manager/sysUser/sysUser_aclTree.html
     */
    @RequestMapping("/toSysUser_aclTree")
    public String toSysUser_aclTree(@RequestParam("sysUserId")int sysUserId, Model model) {
        model.addAttribute("sysUserId", sysUserId);
        return "manager/sysUser/sysUser_aclTree.html";
    }
    
    /**
     * 获取该管理员的系统权限tree
     * @param sysUserId 管理员账号id
     * @return JsonData
     */
    @RequestMapping("/sysUserAclTree")
    @ResponseBody
    public JsonData sysUserAclTree(@RequestParam("sysUserId")int sysUserId) {
        List<SysAcl> listSysAcl = sysUserManagerService.sysUserAclTree(sysUserId);
        return JsonData.success(listSysAcl);
    }
    
    /**
     * 添加或者移除受限url
     * @param banUrl 受限url
     * @param reBanUrl 移除的受限url
     * @param sysUserId 管理员账号id
     * @return JsonData
     */
    @RequestMapping("/updateBanUrl")
    @ResponseBody
    public JsonData updateBanUrl(String[] addBanUrl
            , String[] delBanUrl, @RequestParam("sysUserId")int sysUserId) {
        sysUserManagerService.updateBanUrl(addBanUrl, delBanUrl, sysUserId);
        return JsonData.success();
    }
}
